package com.grj.service.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.grj.model.user.convert.UserConvert;
import com.grj.model.user.dto.UserDTO;
import com.grj.model.user.entity.User;
import com.grj.service.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @author 14505
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;
    @RequestMapping("/test")
    public List<UserDTO> test() {
        UserDTO eren = UserConvert.INSTANCE.dto(new User(1, "eren"));
        return Arrays.asList(eren,eren);
    }

    @PostMapping("/info")
    public UserDTO info(@RequestBody User user, HttpServletRequest request) {
        System.out.println(request.getHeader("uid"));
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        return UserConvert.INSTANCE.dto(userService.getOne(queryWrapper));
    }

    @GetMapping("/info")
    public UserDTO info(HttpServletRequest request) {
        System.out.println(request.getHeader("uid"));
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId, request.getHeader("uid"));
        return UserConvert.INSTANCE.dto(userService.getOne(queryWrapper));
    }
}
